home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 108_01 / dmf.c < prev    next >
Text File  |  1985-11-13  |  8KB  |  302 lines

  1.  
  2. /**********************************************************
  3.  ***                            ***
  4.  ***    Copyright (c) 1981 by David M. Fogg        ***
  5.  ***                            ***
  6.  ***        2632 N.E. Fremont            ***
  7.  ***        Portland, OR 97212            ***
  8.  ***                            ***
  9.  ***        (503) 288-3502{HM} || 223-8033{WK}        ***
  10.  ***                            ***
  11.  ***    Permission is herewith granted for non-     ***
  12.  ***    commercial distribution through the BDS C    ***
  13.  ***    User's Group; any and all forms of commercial   ***
  14.  ***    redistribution are strenuously unwished-for.    ***
  15.  ***                            ***
  16.  **********************************************************/
  17.  
  18. /* ---> DMF.C <--- : personal BDS C system library functions
  19.  
  20.    15 OCT 80: convert from WhiteC version
  21.    23 Oct: c/STRCMP/CMPSTR/ 2 elim BDS lib conflict; + INSTR
  22.    26 Oct: + COIN, ZCHAR
  23.    29 Oct: + OCLOSE: housekeeping close buff ofile
  24.    4 Nov: NDC updates/fixes: add igcase to CMPSTR & to calls
  25.         in QUICK, REHEAP; fix REHEAP; fix OCLOSE
  26.    19 Nov: + INSET: as INSTR, but 2nd arg is char (kinda sim Pascal sets)
  27.    20 Nov: + GETYN: prompt/get Y/N answer
  28.    22 Nov: + GETFIL: get/check filename
  29.    25 Nov: chg COIN 2.X BDOS call -> X.X BIOS call
  30. */
  31.  
  32. #include <std.h>
  33.  
  34. /* NB: modules in alphabetical order */
  35.  
  36. /*------------------------------------------------------------*/
  37. cmpstr (s, t, bf, ic)             /* --<COMPARE 2 STRINGS>-- */
  38. char *s, *t;   /* pointers to strings */
  39. BOOL bf;       /* backward compare flag */
  40. BOOL ic;       /* ignore case flag */
  41. /*
  42.     RETURNS: <0 if s<t, 0 if s==t, >0 if s>t
  43.         (if bf==YES, s & t are switched)
  44.            (if ic==YES, s & t are tolowered)
  45. */
  46. {
  47.    if (ic) {
  48.       for (; tolower(*s) == tolower(*t); s++, t++)
  49.      if (*s == NULL)
  50.         return (0);
  51.       return (bf ? tolower(*t) - tolower(*s) : tolower(*s) - tolower(*t));
  52.    }
  53.    else {
  54.       for (; *s == *t; s++, t++)
  55.      if (*s == NULL)
  56.         return (0);
  57.       return (bf ? *t - *s : *s - *t);
  58.    }
  59. }
  60.  
  61.  
  62. /*------------------------------------------------------------*/
  63. char coin ()           /* --<NON-ECHOING CONSOLE INPUT>-- */
  64. {
  65.    char c;
  66.  
  67.    return (bios(CONIN, 0));
  68. }
  69.  
  70.  
  71. /*------------------------------------------------------------*/
  72. errxit (msg)           /* --<PRINT MESSAGE & BOMB>-- */
  73. char *msg;
  74. {
  75.    puts(VIDINV);
  76.    puts("\007** "); puts(msg); puts(" **\007\n");
  77.    puts(VIDNOR);
  78.    exit ();
  79. }
  80.  
  81.  
  82. /*------------------------------------------------------------*/
  83. getfil (prom, name)    /* --<GET/CHECK FILE name>-- */
  84. char *prom, *name;
  85. {
  86.    int fd;
  87.    char tnam[MAXLINE];
  88.  
  89.    do {
  90.       printf("%s: ", prom); gets(tnam);
  91.    } while (strlen(tnam) > MAXNAM);
  92.    strcpy(name, tnam);
  93.  
  94.    fd = open(name, READ); close(fd);
  95.    return(fd);
  96. }
  97.  
  98.  
  99. /*------------------------------------------------------------*/
  100. BOOL getyn (x, y, prom)     /* --<PROMPT 4 Y/N ANSWER>-- */
  101. int x,y;
  102. char *prom;
  103. {
  104.    char ans;
  105.  
  106.    if (x != 0) toxy(x, y);    /* set x=0 2 skip cursor pos */
  107.    printf("%s (Y/N)? ", prom);
  108.  
  109.    do {
  110.       ans = toupper(coin());
  111.    } while (ans != 'Y' && ans != 'N');
  112.    putchar(ans); putchar('\n');
  113.  
  114.    return (ans == 'Y' ? YES : NO);
  115. }
  116.  
  117.  
  118. /*------------------------------------------------------------*/
  119. imax (a, b)            /* --<RETURN int MAX OF a & b>-- */
  120. int a, b;
  121. {
  122.    return (b > a ? b : a);
  123. }
  124.  
  125.  
  126. /*------------------------------------------------------------*/
  127. imin (a, b)            /* --<RETURN int MIN OF a & b>-- */
  128. int a, b;
  129. {
  130.    return (b < a ? b : a);
  131. }
  132.  
  133.  
  134. /*------------------------------------------------------------*/
  135. inset (str, chr)       /* --<RET POS OF chr IN str>-- */
  136. char *str, chr;
  137.           /* RET 0 if no match, or chr==NULL */
  138. {
  139.    int i, l;
  140.  
  141.    if (chr == NULL) return (0);
  142.  
  143.    l = strlen(str);
  144.    for (i = 0; i < l; ++i)
  145.       if (str[i] == chr) return (++i);
  146.  
  147.    return (0);
  148. }
  149.  
  150.  
  151. /*------------------------------------------------------------*/
  152. instr (str, pat)    /* --<RET CHARPOS OF pat IN str>-- */
  153. char *str, *pat;
  154.           /* RET 0 if no match, or pat == "" */
  155. {
  156.    int ls, lp, i, j;
  157.    BOOL ok;
  158.  
  159.    lp = strlen(pat);
  160.    if (lp == 0) return (0);
  161.  
  162.    ls = strlen(str) - lp + 1;
  163.  
  164.    for (i = 0; i < ls; ++i) {
  165.       ok = YES;
  166.       for (j = 0; j < lp; ++j)
  167.      if (*(str+i+j) != *(pat+j)) {
  168.         ok = NO;
  169.         break;
  170.      }
  171.       if (ok) return (++i);
  172.    }
  173.    return (0);
  174. }
  175.  
  176.  
  177. /*------------------------------------------------------------*/
  178. oclose (iobuf)              /* --<CLOSE BUFF O/P FILE>-- */
  179. char *iobuf;
  180.           /* & do all housekeeping */
  181. {
  182.    putc(CPMEOF, iobuf);
  183.    fflush(iobuf);
  184.    fclose(iobuf);
  185. }
  186.  
  187.  
  188. /*------------------------------------------------------------*/
  189. quick(linptr, bot, top, back, igcas)           /* --<QUICKSORT>-- */
  190.  
  191. char **linptr; /* base of array of line pointers */
  192. int bot, top;  /* range of elements to sort */
  193. BOOL back;     /* if YES, sort in descending order */
  194. BOOL igcas;    /* if YES, ignore case */
  195. /*
  196.     This routine calls itself recursively to sort
  197.     the text lines pointed at by the segment of
  198.     linptr between elements top and bot.
  199. */
  200. {
  201.    char *pivlin;    /* pointer to pivot line */
  202.    int lo;
  203.    int hi;
  204.    int range;
  205.    
  206.    lo = bot - 1;
  207.    hi = top;
  208.    range = top - bot;
  209.  
  210.    if (range <= 0)        /* aught to do? */
  211.       return;
  212.  
  213.    if (range == 1) {        /* only 2 elts? */
  214.       if (cmpstr(linptr[bot], linptr[top], back, igcas) > 0)
  215.      swap(&linptr[bot], &linptr[top]);
  216.       return;
  217.    }
  218.  
  219.    swap(&linptr[top], &linptr[bot + range / 2]);
  220.    pivlin = linptr[top];        /* set pivot line */
  221.  
  222.    while (lo < hi) {        /* QUICKSORT */
  223.       for (++lo; cmpstr(linptr[lo], pivlin, back, igcas) < 0; ++lo)
  224.      ;
  225.       for (--hi; hi > lo && cmpstr(linptr[hi], pivlin, back, igcas) > 0; --hi)
  226.      ;
  227.       if (lo < hi)
  228.      swap(&linptr[lo], &linptr[hi]);
  229.    }
  230.    swap(&linptr[lo], &linptr[top]);    /* put pivot in midl */
  231.  
  232.    hi = lo;                /* fat pivot section */
  233.    while (lo-- > bot && cmpstr(linptr[lo + 1], linptr[lo], back, igcas) == 0)
  234.       ;
  235.    while (hi++ < top && cmpstr(linptr[hi - 1], linptr[hi], back, igcas) == 0)
  236.       ;
  237.  
  238.    if (lo - bot < top - hi) {    /* sort shorter segment first */
  239.       quick(linptr, bot, lo, back, igcas);
  240.       quick(linptr, hi, top, back, igcas);
  241.    }
  242.    else {
  243.       quick(linptr, hi, top, back, igcas);
  244.       quick(linptr, bot, lo, back, igcas);
  245.    }
  246. }
  247.  
  248.  
  249. /*--------------------------------------------------------------*/
  250. reheap (linps, nlins, back, igcas)     /* --<RE-FORM A HEAP>-- */
  251. char *linps[]; /* pointers to lines */
  252. int nlins;     /* # of lines */
  253. BOOL back;     /* backward (=descending) collating sequence flag */
  254. BOOL igcas;    /* ignore case flag */
  255. /*
  256.     The first line pointed to by linps is trickled down
  257.     until it is in place; the rest of the lines are
  258.     assumed to be in proper order.
  259. */
  260. {
  261.    int i;
  262.  
  263.    for (i = 1; i < nlins; ++i)
  264.       if (cmpstr(linps[i-1], linps[i], back, igcas) > 0)
  265.      swap(&linps[i-1], &linps[i]);
  266.       else
  267.      break;
  268. }
  269.  
  270.  
  271. /*------------------------------------------------------------*/
  272. swap (a, b)            /* --<EXCHANGE 2 STRING POINTERS>-- */
  273. char **a, **b;    /* pointers to string pointers */
  274. {
  275.    char *t;
  276.  
  277.    t = *a;
  278.    *a = *b;
  279.    *b = t;
  280. }
  281.  
  282.  
  283. /*------------------------------------------------------------*/
  284. toxy (x, y)            /* --<POSITION CURSOR TO X,Y>-- */
  285. char x, y;
  286.                  /* --> Zenith Z-19 Version <-- */
  287. {
  288.    x = abs(x) % HSIZ + 32;
  289.    y = abs(y) % VSIZ + 32;
  290.  
  291.    puts(CURLEA); putchar(y); putchar(x);
  292. }
  293. /*------------------------------------------------------------*/
  294. char zchar ()             /* --<GET Z-19 KB CHAR>-- */
  295.  
  296.             /* --> RET LC IF FUNKEY, ELSE UC <-- */
  297. {
  298.    char c;
  299.  
  300.    return (((c = coin()) == ESC) ? tolower(coin()) : toupper(c));
  301. }
  302.